app_family.controller("controller_datahealthroomchart", function($scope, $rootScope, httpRequest, $location) {
	$rootScope.$broadcast('currentModule', 'controller_datahealthroomchart');
	$rootScope.$broadcast('footerFlag', false); // 是否显示底部导航
	$scope.imagebasepath = imagebasepath;
	data_sessionStorage = JSON.parse(sessionStorage.getItem('data_sessionStorage'));
	$scope.type = 'week';
	
	/**
	 * 判断是否用手机登录过
	 */
	if (sessionStorage.getItem('data_sessionStorage')) {
		var familyPhone = data_sessionStorage.familyInfo.phone;
		if (familyPhone == '' || familyPhone == null) {
			window.location.replace(basepath + 'login.html');
			return;
		}
	} else {
		window.location.replace(basepath + 'login.html');
		return;
	}
	
	/**
	 * tab点击
	 */
	$scope.showType = function (type) {
		$scope.type = type;
	};
	
	$scope.older_account = data_sessionStorage.data_searchFamilyInfo.older_account; // 账号
	/**
	 * 根据账号，查询个人信息
	 */ 
	httpRequest.getOlderInfo({
		account : $scope.older_account
	}, function (data) {
		if (data.success === true) {
			$scope.olderInfoObj = data.obj;
		} else if (data.success === false) {
			$.dialog.showToast({msgTxt : data.msg});
		}
	});
	
	/**
	 * 查询图表数据
	 * timerType 1-周，2-月
	 */
	$scope.searchLocus = function (type, timerType, containerId) {
		httpRequest.searchLocus({
			account : $scope.older_account,
			type : type,
			timerType : timerType
		}, function (data) {
			if (data.success === true) {
				$scope.drawGraph(data, type, timerType, containerId); // 本周数据
			} else if (data.success === false) {
				$.dialog.showToast({msgTxt : data.msg});
			}
		});
	};
	
	/**
	 * 格式化日期
	 * @param strTime 日期时间字符串
	 * @returns 年月日时分（2016/06/21 18:30）
	 */
	$scope.formatDateMMDD = function(strTime) {
	    var date = new Date(strTime);
	    var minutes = "";
	    if (0 == date.getMinutes()) {
	    	minutes = "00";
	    } else {
	    	minutes = date.getMinutes();
	    }
	    return (date.getMonth() + 1) + "/" + date.getDate();
	};
	
	/**
	 * 拼装参考标准值
	 * standValue 参考值对象
	 * type 1：返回参考值对象的value1和value2值；2：返回参考值对象的value3和value4值；
	 */
	$scope.getStandValue = function(standValue, type) {
		var result = "";
		if (1 == type) {
			// 有value1和value2标准值
			if (null != standValue.values_1 && "" != standValue.values_1
					&& null != standValue.values_2 && "" != standValue.values_2) {
				result = "(" + standValue.values_1 + "-" + standValue.values_2
							+ standValue.standard_unit_name + ")";
			}
		} else if (2 == type) {
			// 有value3和value4标准值
			if (null != standValue.values_3 && "" != standValue.values_3
					&& null != standValue.values_4 && "" != standValue.values_4) {
				result = "(" + standValue.values_3 + "-" + standValue.values_4
							+ standValue.standard_unit_name + ")";
			}
		}
		return result;
	};
	
	/**
	 * 画统计图
	 */
	$scope.drawGraph = function (data, type, dateType, containerId) {
		// X轴名称
		var categories = new Array();
		// 曲线值
		var series = new Array();
		// 护理数据列表
		var userCareInfoList = data.obj.userCareInfoList;
		// 参考标准值
		var standValue = data.obj.standerEntity;
		// 数值1
		var value1 = new Array();
		// 数值2
		var value2 = new Array();
		// 曲线1对象
		var graph1 = new Object();
		// 曲线2对象
		var graph2 = new Object();
		
		// X轴显示的最大项
		var max = 7;
		
		// 按月查看
		if ("2" == dateType) {
			// 按月查看的X轴
			categories = [];
			for (var i = 1; i < 13; i++) {
				categories.push(i + '月');
			}
			
			// 按月查看 X轴显示的最大项
			max = 11;
			
			// Y轴处理
			var value1_name = '', value2_name = '';
			if ("30004" == type || "30005" == type || "30001" == type) {
				if ("30004" == type) { // 血压
					graph1.name = "高压" + $scope.getStandValue(standValue, 1);
					graph2.name = "低压" + $scope.getStandValue(standValue, 2);
				} 
				
				if ("30005" == type) { // 血糖
					graph1.name = "空腹" + $scope.getStandValue(standValue, 1);
					graph2.name = "餐后" + $scope.getStandValue(standValue, 2);
				}
				
				if ("30001" == type) { // 体温
					graph1.name = "腋下" + $scope.getStandValue(standValue, 1);
					graph2.name = "口腔" + $scope.getStandValue(standValue, 2);
				}
				
				for (var i = 1; i < 13; i++) {
					var flag = false;
					
					for (var j = 0; null != userCareInfoList && j < userCareInfoList.length; j++) {
						if (i == parseInt(userCareInfoList[j].input_time)) {
							value1.push(parseFloat(userCareInfoList[j].care_value1));
							value2.push(parseFloat(userCareInfoList[j].care_value2));
							flag = true;
							break;
						}
					}
					
					if (!flag) {
						value1.push('');
						value2.push('');
					}
				}
				
				graph1.data = value1;
				graph2.data = value2;
				series.push(graph1);
				series.push(graph2);
			} else {
				// 其它曲线绘制
				graph1.name = "标准" + $scope.getStandValue(standValue, 1);
				for (var i = 1; i < 13; i++) {
					var flag = false;
					
					for (var j = 0; null != userCareInfoList && j < userCareInfoList.length; j++) {
						if (i == parseInt(userCareInfoList[j].input_time)) {
							value1.push(parseFloat(userCareInfoList[j].care_value1));
							flag = true;
							break;
						}
					}
					
					if (!flag) {
						value1.push('');
					}
				}
				
				graph1.data = value1;
				series.push(graph1);
			}
		}
		// 按周查看
		if ("1" == dateType) {
			for (var i = 0; null != userCareInfoList && i < userCareInfoList.length; i++) {
				var datetime = "";
				// 设置周时间的显示格式
				if ("1" == dateType) {
					datetime = $scope.formatDateMMDD(userCareInfoList[i].input_time);
					categories.push(datetime);
				}
			
				// 值不为空
				if (null != userCareInfoList[i].care_value1) {
					value1.push(parseFloat(userCareInfoList[i].care_value1));
				} else {
					value1.push("");
				}
				if (null != userCareInfoList[i].care_value2) {
					value2.push(parseFloat(userCareInfoList[i].care_value2));
				} else {
					value2.push("");
				}
			}
			if ("30004" == type) { // 血压
				graph1.name = "高压" + $scope.getStandValue(standValue, 1);
				graph2.name = "低压" + $scope.getStandValue(standValue, 2);
				graph1.data = value1;
				graph2.data = value2;
				series.push(graph1);
				series.push(graph2);
			} else if ("30005" == type) { // 血糖
				graph1.name = "空腹" + $scope.getStandValue(standValue, 1);
				graph2.name = "餐后" + $scope.getStandValue(standValue, 2);
				
				graph1.data = value1;
				graph2.data = value2;
				series.push(graph1);
				series.push(graph2);
			} else if ("30001" == type) { // 体温
				graph1.name = "腋下" + $scope.getStandValue(standValue, 1);
				graph2.name = "口腔" + $scope.getStandValue(standValue, 2);
				graph1.data = value1;
				graph2.data = value2;
				series.push(graph1);
				series.push(graph2);
			} else {
				// 其它曲线绘制
				graph1.name = "标准" + $scope.getStandValue(standValue, 1);
				graph1.data = value1;
				series.push(graph1);
			}
		}
		
		// 调用第三方图形组件，渲染曲线图
		$('#' + containerId).highcharts({
	        chart: {
	            type: 'line',
	            spacing : [20, 20, 10, 10]
	        },
	        colors : ['#ff2d55', '#00b0ff'],
	        title: {
	            text: ''
	        },
	        credits : {
	        	text : ''
	        },
	        scrollbar: {
	        	enabled: true
	        },
	        xAxis: {
	            categories: categories,
	            max : max,
	            gridLineColor: '#ccc',//纵向网格线颜色
		        gridLineWidth: 1, //纵向网格线宽度
		        labels: {
                	rotation : 0,
                    style: {
                        fontSize:'10px'
                    }
                }
	        },
	        yAxis: {
	            title: {
	                text: ''
	            }
	        },
	        tooltip: {
	            enabled: false,
	            formatter: function() {
	                return '<b>'+ this.series.name +'</b><br/>'+this.x +': '+ this.y +'°C';
	            }
	        },
	        plotOptions: {
	            line: {
	                dataLabels: {
	                    enabled: true
	                },
	                enableMouseTracking: false
	            }
	        },
	        series: series
	    });
	};
	
	/**
	 * 页面初始化 查询图表
	 */
	$scope.searchLocus($('ul#weekChartType li').eq(0).attr('carenum'), '1', 'weekChart');   // 本周数据
	$scope.searchLocus($('ul#weekChartType li').eq(0).attr('carenum'), '2', 'monthChart');   // 本月数据
	
	
	$('ul#weekChartType').delegate('li', 'click', function () {
		var target = $(this);
		target.attr('class', 'active').siblings('li').attr('class', '');
		$scope.searchLocus(target.attr('carenum'), '1', 'weekChart');
	});
	$('ul#monthChartType').delegate('li', 'click', function () {
		var target = $(this);
		target.attr('class', 'active').siblings('li').attr('class', '');
		$scope.searchLocus(target.attr('carenum'), '2', 'monthChart');
	});
	
});